package com.masterclient.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.eoner.baselibrary.utils.ScreenUtils;

/**
 * 作者：sean
 * 创建时间：2017/7/11 17:57
 * 功能说明：
 */

public class PersonalHeadLayout extends RelativeLayout{
    private Context context;
    private Paint paint,paint2;
    private Path path;
    private int screenW;
    private RectF rectF;

    private int originW;
    private int originH;

    private float topH;

    private int viewW;
    private int viewH;
    private boolean isFirst = true;
    public PersonalHeadLayout(@NonNull Context context) {
        super(context);
    }

    public PersonalHeadLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        if(isFirst){
            originW = w;
            originH = h;
            isFirst = false;
        }

        viewW = w;
        viewH = h;


        topH = viewH*0.38f+(viewH-originH)*1.2f;

        path = new Path();
        path.moveTo(0,originH*0.7499f);
        path.quadTo(screenW/2,topH,screenW,originH*0.7499f);
        path.moveTo(screenW,originH*0.7499f);
        path.lineTo(0,viewH*0.7499f);
        path.moveTo(0,viewH*0.7499f);
        path.close();

        rectF = new RectF(0,originH*0.75f,screenW,viewH);
        if(onSizeChangeListerner!=null){
            onSizeChangeListerner.onLineTopChange((int) (topH+originH*0.75f)/2);
        }
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void init() {
        screenW = ScreenUtils.getScreenWidth();
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);

        paint2 = new Paint();
        paint2.setStyle(Paint.Style.FILL);
        paint2.setColor(Color.TRANSPARENT);
        paint2.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(topH>originH*0.75f){
            canvas.save();
            canvas.clipPath(path, Region.Op.DIFFERENCE);
            canvas.drawRect(rectF,paint);
            canvas.restore();
        }else {
            canvas.drawRect(rectF,paint);
            canvas.drawPath(path,paint);
        }

        super.onDraw(canvas);
    }

    public void setOnSizeChangeListerner(OnSizeChangeListerner onSizeChangeListerner) {
        this.onSizeChangeListerner = onSizeChangeListerner;
    }

    private OnSizeChangeListerner onSizeChangeListerner;
    public interface OnSizeChangeListerner{
        public void onLineTopChange(int top);
    }
}
